package com.example.demo.controller;

import com.example.demo.model.Friend;
import com.example.demo.model.User;
import com.example.demo.myBatisMapper.FriendMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendController {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    public Object getFriendList(HttpServletRequest request){
        //1.先从会话中获取到当前登录用户的userId
        HttpSession session = request.getSession(false);
        if (session == null){
            //当前会话不存在,未登录
            System.out.println("[getFriendList] 会话不存在!");
            return new ArrayList<Friend>();
        }
        User user =(User) session.getAttribute("user");
        if (user == null){
            //当前用户对象未在会话中
            System.out.println("[getFriendList] user不存在!");
            return new ArrayList<Friend>();
        }
        //2.根据userId从数据库查询数据
        List<Friend> list = friendMapper.selectFriendList(user.getUserId());
        return list;
    }
}
